Mapeo de COVID-19¶
Base de datos¶
In [2]:
import pandas as pd
df = pd.read_csv("C:/Users/camil/Desktop/Cato/ciclo 10/Herramientas cuantitativas/maps/positivos_covid.csv", delimiter=';')
In [3]:
print(df.head())
FECHA_CORTE DEPARTAMENTO PROVINCIA DISTRITO METODODX EDAD SEXO \ 0 20241203 TUMBES TUMBES TUMBES AG 46.0 FEMENINO 1 20241203 LIMA LIMA JESUS MARIA AG 69.0 FEMENINO 2 20241203 SAN MARTIN MOYOBAMBA MOYOBAMBA AG 55.0 FEMENINO 3 20241203 AREQUIPA CAYLLOMA COPORAQUE AG 50.0 MASCULINO 4 20241203 LIMA LIMA JESUS MARIA AG 58.0 MASCULINO FECHA_RESULTADO UBIGEO id_persona 0 20221207.0 240101.0 203499.0 1 20230822.0 150113.0 221397.0 2 20240108.0 220101.0 295651.0 3 20230824.0 40506.0 851625.0 4 20221217.0 150113.0 287786.0
In [4]:
df=df[df.EDAD>50]
In [5]:
df.METODODX.value_counts()
Out[5]:
METODODX AG 599842 PCR 400134 PR 293382 Name: count, dtype: int64
In [6]:
# NUEVA VARIABLE "AÑO"
df['FECHA_RESULTADO'] = pd.to_datetime(df['FECHA_RESULTADO'], format='%Y%m%d', errors='coerce')
In [7]:
df['AÑO'] = df['FECHA_RESULTADO'].dt.year
In [8]:
df.AÑO.head()
Out[8]:
1 2023.0 2 2024.0 4 2022.0 7 2023.0 9 2020.0 Name: AÑO, dtype: float64
In [9]:
df['AÑO'] = df['AÑO'].astype('Int64') #a numeros enteros
In [10]:
print(df[['AÑO']].head())
AÑO 1 2023 2 2024 4 2022 7 2023 9 2020
Agreggate¶
In [11]:
# AG: Antigenos
# PCR: Prueba molecular
# PR: Prueba rápida serológica
In [12]:
df = df[df['AÑO'] != 1899]
In [13]:
df = df[df['PROVINCIA'] != 'EN INVESTIGACIÓN']
In [14]:
indexList=['AÑO','DEPARTAMENTO','PROVINCIA','METODODX']
aggregator={'METODODX':[len]}
prov=df.groupby(indexList,observed=True).agg(aggregator)
prov
Out[14]:
| METODODX | ||||
|---|---|---|---|---|
| len | ||||
| AÑO | DEPARTAMENTO | PROVINCIA | METODODX | |
| 2020 | AMAZONAS | BAGUA | PCR | 203 |
| PR | 2368 | |||
| BONGARA | PCR | 20 | ||
| PR | 92 | |||
| CHACHAPOYAS | PCR | 90 | ||
| ... | ... | ... | ... | ... |
| 2024 | TUMBES | TUMBES | PCR | 15 |
| ZARUMILLA | PCR | 6 | ||
| UCAYALI | CORONEL PORTILLO | AG | 16 | |
| PCR | 17 | |||
| PADRE ABAD | AG | 1 |
1886 rows × 1 columns
In [15]:
#wide
Draft=prov.unstack(3).fillna(0) #leftmost index in rows
Draft
Out[15]:
| METODODX | |||||
|---|---|---|---|---|---|
| len | |||||
| METODODX | AG | PCR | PR | ||
| AÑO | DEPARTAMENTO | PROVINCIA | |||
| 2020 | AMAZONAS | BAGUA | 0.0 | 203.0 | 2368.0 |
| BONGARA | 0.0 | 20.0 | 92.0 | ||
| CHACHAPOYAS | 0.0 | 90.0 | 419.0 | ||
| CONDORCANQUI | 0.0 | 14.0 | 540.0 | ||
| LUYA | 0.0 | 1.0 | 89.0 | ||
| ... | ... | ... | ... | ... | ... |
| 2024 | TUMBES | CONTRALMIRANTE VILLAR | 0.0 | 4.0 | 0.0 |
| TUMBES | 11.0 | 15.0 | 0.0 | ||
| ZARUMILLA | 0.0 | 6.0 | 0.0 | ||
| UCAYALI | CORONEL PORTILLO | 16.0 | 17.0 | 0.0 | |
| PADRE ABAD | 1.0 | 0.0 | 0.0 | ||
920 rows × 3 columns
In [16]:
# % de personas con prueba AG
Draft['AG_pct']=Draft.iloc[:,1]/(Draft.iloc[:,0] + Draft.iloc[:,1])
prov_Ag=Draft['AG_pct'].unstack('AÑO').fillna(0)
prov_Ag
Out[16]:
| AÑO | 2020 | 2021 | 2022 | 2023 | 2024 | |
|---|---|---|---|---|---|---|
| DEPARTAMENTO | PROVINCIA | |||||
| AMAZONAS | BAGUA | 1.000000 | 0.144654 | 0.233813 | 0.240000 | 0.470588 |
| BONGARA | 1.000000 | 0.291971 | 0.466019 | 0.454545 | 0.928571 | |
| CHACHAPOYAS | 1.000000 | 0.448919 | 0.414784 | 0.219178 | 0.322581 | |
| CONDORCANQUI | 1.000000 | 0.033333 | 0.025000 | 0.000000 | 0.000000 | |
| LUYA | 1.000000 | 0.036585 | 0.059361 | 0.111111 | 0.750000 | |
| ... | ... | ... | ... | ... | ... | ... |
| TUMBES | ZARUMILLA | 0.923077 | 0.315556 | 0.492958 | 0.588235 | 1.000000 |
| UCAYALI | ATALAYA | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| CORONEL PORTILLO | 0.996875 | 0.186831 | 0.503120 | 0.545455 | 0.515152 | |
| PADRE ABAD | 0.000000 | 0.078512 | 0.193694 | 0.000000 | 0.000000 | |
| PURUS | 0.000000 | 0.000000 | 0.000000 | 1.000000 | 0.000000 |
196 rows × 5 columns
In [17]:
#data type
prov_Ag.columns #esta como numero los años
Out[17]:
Index([2020, 2021, 2022, 2023, 2024], dtype='Int64', name='AÑO')
In [18]:
prov_Ag.columns=['year'+str(x) for x in prov_Ag.columns]
In [19]:
prov_Ag
Out[19]:
| year2020 | year2021 | year2022 | year2023 | year2024 | ||
|---|---|---|---|---|---|---|
| DEPARTAMENTO | PROVINCIA | |||||
| AMAZONAS | BAGUA | 1.000000 | 0.144654 | 0.233813 | 0.240000 | 0.470588 |
| BONGARA | 1.000000 | 0.291971 | 0.466019 | 0.454545 | 0.928571 | |
| CHACHAPOYAS | 1.000000 | 0.448919 | 0.414784 | 0.219178 | 0.322581 | |
| CONDORCANQUI | 1.000000 | 0.033333 | 0.025000 | 0.000000 | 0.000000 | |
| LUYA | 1.000000 | 0.036585 | 0.059361 | 0.111111 | 0.750000 | |
| ... | ... | ... | ... | ... | ... | ... |
| TUMBES | ZARUMILLA | 0.923077 | 0.315556 | 0.492958 | 0.588235 | 1.000000 |
| UCAYALI | ATALAYA | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| CORONEL PORTILLO | 0.996875 | 0.186831 | 0.503120 | 0.545455 | 0.515152 | |
| PADRE ABAD | 0.000000 | 0.078512 | 0.193694 | 0.000000 | 0.000000 | |
| PURUS | 0.000000 | 0.000000 | 0.000000 | 1.000000 | 0.000000 |
196 rows × 5 columns
In [20]:
prov_Ag.reset_index(inplace=True)
prov_Ag
Out[20]:
| DEPARTAMENTO | PROVINCIA | year2020 | year2021 | year2022 | year2023 | year2024 | |
|---|---|---|---|---|---|---|---|
| 0 | AMAZONAS | BAGUA | 1.000000 | 0.144654 | 0.233813 | 0.240000 | 0.470588 |
| 1 | AMAZONAS | BONGARA | 1.000000 | 0.291971 | 0.466019 | 0.454545 | 0.928571 |
| 2 | AMAZONAS | CHACHAPOYAS | 1.000000 | 0.448919 | 0.414784 | 0.219178 | 0.322581 |
| 3 | AMAZONAS | CONDORCANQUI | 1.000000 | 0.033333 | 0.025000 | 0.000000 | 0.000000 |
| 4 | AMAZONAS | LUYA | 1.000000 | 0.036585 | 0.059361 | 0.111111 | 0.750000 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 191 | TUMBES | ZARUMILLA | 0.923077 | 0.315556 | 0.492958 | 0.588235 | 1.000000 |
| 192 | UCAYALI | ATALAYA | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| 193 | UCAYALI | CORONEL PORTILLO | 0.996875 | 0.186831 | 0.503120 | 0.545455 | 0.515152 |
| 194 | UCAYALI | PADRE ABAD | 0.000000 | 0.078512 | 0.193694 | 0.000000 | 0.000000 |
| 195 | UCAYALI | PURUS | 0.000000 | 0.000000 | 0.000000 | 1.000000 | 0.000000 |
196 rows × 7 columns
MAPA¶
In [21]:
mapLink='https://github.com/SocialAnalytics-StrategicIntelligence/GeoDF_Analytics/raw/main/maps/ProvsINEI2023.zip'
#panadas vienen como zip hy, r no puede abrir por si acaso
import geopandas as gpd
provmap=gpd.read_file(mapLink)
provmap.info()
<class 'geopandas.geodataframe.GeoDataFrame'> RangeIndex: 196 entries, 0 to 195 Data columns (total 6 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 OBJECTID 196 non-null float64 1 CCDD 196 non-null object 2 CCPP 196 non-null object 3 DEPARTAMEN 196 non-null object 4 PROVINCIA 196 non-null object 5 geometry 196 non-null geometry dtypes: float64(1), geometry(1), object(4) memory usage: 9.3+ KB
In [22]:
provmap['location']=['+'.join(x[0]) for x in zip(provmap.iloc[:,3:5].values)]
provmap.head(10)
Out[22]:
| OBJECTID | CCDD | CCPP | DEPARTAMEN | PROVINCIA | geometry | location | |
|---|---|---|---|---|---|---|---|
| 0 | 1.0 | 01 | 01 | AMAZONAS | CHACHAPOYAS | POLYGON ((-77.72614 -5.94354, -77.72486 -5.943... | AMAZONAS+CHACHAPOYAS |
| 1 | 2.0 | 01 | 02 | AMAZONAS | BAGUA | POLYGON ((-78.61909 -4.51001, -78.61802 -4.510... | AMAZONAS+BAGUA |
| 2 | 3.0 | 01 | 03 | AMAZONAS | BONGARA | POLYGON ((-77.72759 -5.1403, -77.72361 -5.1406... | AMAZONAS+BONGARA |
| 3 | 4.0 | 01 | 04 | AMAZONAS | CONDORCANQUI | POLYGON ((-77.81399 -2.99278, -77.81483 -2.995... | AMAZONAS+CONDORCANQUI |
| 4 | 5.0 | 01 | 05 | AMAZONAS | LUYA | POLYGON ((-78.13023 -5.9037, -78.13011 -5.9041... | AMAZONAS+LUYA |
| 5 | 6.0 | 01 | 06 | AMAZONAS | RODRIGUEZ DE MENDOZA | POLYGON ((-77.44452 -6.05002, -77.44387 -6.050... | AMAZONAS+RODRIGUEZ DE MENDOZA |
| 6 | 7.0 | 01 | 07 | AMAZONAS | UTCUBAMBA | POLYGON ((-78.09288 -5.36258, -78.09288 -5.364... | AMAZONAS+UTCUBAMBA |
| 7 | 8.0 | 02 | 01 | ANCASH | HUARAZ | POLYGON ((-77.3987 -9.35563, -77.39852 -9.3560... | ANCASH+HUARAZ |
| 8 | 9.0 | 02 | 02 | ANCASH | AIJA | POLYGON ((-77.61368 -9.649, -77.61241 -9.64975... | ANCASH+AIJA |
| 9 | 10.0 | 02 | 03 | ANCASH | ANTONIO RAYMONDI | POLYGON ((-77.08856 -8.97496, -77.08804 -8.975... | ANCASH+ANTONIO RAYMONDI |
In [23]:
print(prov_Ag.columns)
Index(['DEPARTAMENTO', 'PROVINCIA', 'year2020', 'year2021', 'year2022',
'year2023', 'year2024'],
dtype='object')
In [24]:
# Unir las columnas 'departamento' y 'provincia' en una nueva columna 'location'
prov_Ag['location'] = prov_Ag.apply(lambda row: f"{row['DEPARTAMENTO']} + {row['PROVINCIA']}", axis=1)
# Ver las primeras filas para confirmar
prov_Ag.head()
Out[24]:
| DEPARTAMENTO | PROVINCIA | year2020 | year2021 | year2022 | year2023 | year2024 | location | |
|---|---|---|---|---|---|---|---|---|
| 0 | AMAZONAS | BAGUA | 1.0 | 0.144654 | 0.233813 | 0.240000 | 0.470588 | AMAZONAS + BAGUA |
| 1 | AMAZONAS | BONGARA | 1.0 | 0.291971 | 0.466019 | 0.454545 | 0.928571 | AMAZONAS + BONGARA |
| 2 | AMAZONAS | CHACHAPOYAS | 1.0 | 0.448919 | 0.414784 | 0.219178 | 0.322581 | AMAZONAS + CHACHAPOYAS |
| 3 | AMAZONAS | CONDORCANQUI | 1.0 | 0.033333 | 0.025000 | 0.000000 | 0.000000 | AMAZONAS + CONDORCANQUI |
| 4 | AMAZONAS | LUYA | 1.0 | 0.036585 | 0.059361 | 0.111111 | 0.750000 | AMAZONAS + LUYA |
In [25]:
## Limpieza
In [26]:
!pip install unidecode
Requirement already satisfied: unidecode in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (1.3.8)
In [27]:
import unidecode
byePunctuation=lambda x: unidecode.unidecode(x)
prov_Ag['location']=prov_Ag['location'].apply(byePunctuation)
provmap['location']=provmap['location'].apply(byePunctuation)
In [28]:
prov_Ag['location']=prov_Ag.location.str.replace("\-|\_|\s+","",regex=True)
provmap['location']=provmap.location.str.replace("\-|\_|\s+","",regex=True)
<>:1: SyntaxWarning: invalid escape sequence '\-'
<>:2: SyntaxWarning: invalid escape sequence '\-'
<>:1: SyntaxWarning: invalid escape sequence '\-'
<>:2: SyntaxWarning: invalid escape sequence '\-'
C:\Users\camil\AppData\Local\Temp\ipykernel_30244\3744470951.py:1: SyntaxWarning: invalid escape sequence '\-'
prov_Ag['location']=prov_Ag.location.str.replace("\-|\_|\s+","",regex=True)
C:\Users\camil\AppData\Local\Temp\ipykernel_30244\3744470951.py:2: SyntaxWarning: invalid escape sequence '\-'
provmap['location']=provmap.location.str.replace("\-|\_|\s+","",regex=True)
In [29]:
nomatch_df=set(prov_Ag.location)- set(provmap.location)
nomatch_gdf=set(provmap.location)-set(prov_Ag.location)
In [30]:
len(nomatch_df), len(nomatch_gdf) #!!!
Out[30]:
(2, 2)
In [31]:
!pip install thefuzz
Requirement already satisfied: thefuzz in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (0.22.1) Requirement already satisfied: rapidfuzz<4.0.0,>=3.0.0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from thefuzz) (3.9.7)
In [32]:
# pick the closest match from nomatch_gdf for a value in nomatch_df
from thefuzz import process
[(dis,process.extractOne(dis,nomatch_gdf)) for dis in sorted(nomatch_df)]
Out[32]:
[('ANCASH+ANTONIORAIMONDI', ('ANCASH+ANTONIORAYMONDI', 95)),
('ICA+NAZCA', ('ICA+NASCA', 89))]
In [33]:
# Diccionario de cambios
{dis:process.extractOne(dis,nomatch_gdf)[0] for dis in sorted(nomatch_df)}
Out[33]:
{'ANCASH+ANTONIORAIMONDI': 'ANCASH+ANTONIORAYMONDI', 'ICA+NAZCA': 'ICA+NASCA'}
In [34]:
# then:
changesinDF={dis:process.extractOne(dis,nomatch_gdf)[0] for dis in sorted(nomatch_df)}
In [35]:
prov_Ag.replace({'location': changesinDF}, inplace=True)
In [36]:
# comprobamos
In [37]:
nomatch_df=set(prov_Ag.location)- set(provmap.location)
nomatch_gdf=set(provmap.location)-set(prov_Ag.location)
[(dis,process.extractOne(dis,nomatch_gdf)) for dis in sorted(nomatch_df)]
Out[37]:
[]
In [38]:
#merge
prov_Ag_map=provmap.merge(prov_Ag, on='location',how='left',indicator='flag')
In [39]:
prov_Ag_map.info()
<class 'geopandas.geodataframe.GeoDataFrame'> RangeIndex: 196 entries, 0 to 195 Data columns (total 15 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 OBJECTID 196 non-null float64 1 CCDD 196 non-null object 2 CCPP 196 non-null object 3 DEPARTAMEN 196 non-null object 4 PROVINCIA_x 196 non-null object 5 geometry 196 non-null geometry 6 location 196 non-null object 7 DEPARTAMENTO 196 non-null object 8 PROVINCIA_y 196 non-null object 9 year2020 196 non-null float64 10 year2021 196 non-null float64 11 year2022 196 non-null float64 12 year2023 196 non-null float64 13 year2024 196 non-null float64 14 flag 196 non-null category dtypes: category(1), float64(6), geometry(1), object(7) memory usage: 21.9+ KB
In [40]:
prov_Ag_map['flag']=prov_Ag_map.flag.astype(str)
In [41]:
#
bye=['DEPARTAMEN', 'PROVINCIA_x', 'CCPP','CCDD']
prov_Ag_map.drop(columns=bye,inplace=True)
prov_Ag_map.head()
Out[41]:
| OBJECTID | geometry | location | DEPARTAMENTO | PROVINCIA_y | year2020 | year2021 | year2022 | year2023 | year2024 | flag | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1.0 | POLYGON ((-77.72614 -5.94354, -77.72486 -5.943... | AMAZONAS+CHACHAPOYAS | AMAZONAS | CHACHAPOYAS | 1.0 | 0.448919 | 0.414784 | 0.219178 | 0.322581 | both |
| 1 | 2.0 | POLYGON ((-78.61909 -4.51001, -78.61802 -4.510... | AMAZONAS+BAGUA | AMAZONAS | BAGUA | 1.0 | 0.144654 | 0.233813 | 0.240000 | 0.470588 | both |
| 2 | 3.0 | POLYGON ((-77.72759 -5.1403, -77.72361 -5.1406... | AMAZONAS+BONGARA | AMAZONAS | BONGARA | 1.0 | 0.291971 | 0.466019 | 0.454545 | 0.928571 | both |
| 3 | 4.0 | POLYGON ((-77.81399 -2.99278, -77.81483 -2.995... | AMAZONAS+CONDORCANQUI | AMAZONAS | CONDORCANQUI | 1.0 | 0.033333 | 0.025000 | 0.000000 | 0.000000 | both |
| 4 | 5.0 | POLYGON ((-78.13023 -5.9037, -78.13011 -5.9041... | AMAZONAS+LUYA | AMAZONAS | LUYA | 1.0 | 0.036585 | 0.059361 | 0.111111 | 0.750000 | both |
In [42]:
# lidiar con los CEROS
prov_Ag_map.fillna(0,inplace=True)
Guardar el geoDF
In [43]:
#crear carpeta maps, donde guardar
import os
prov_Ag_map.to_file(os.path.join('C:/Users/camil/Desktop/Cato/ciclo 10/Herramientas cuantitativas/maps',"provinciasPeru.gpkg"), layer='provinciasDengue', driver="GPKG")
Explorar variables¶
In [44]:
# statistics
prov_Ag_map.year2022.describe()
Out[44]:
count 196.000000 mean 0.125021 std 0.161102 min 0.000000 25% 0.016327 50% 0.068471 75% 0.170919 max 1.000000 Name: year2022, dtype: float64
In [45]:
! pip install seaborn
Requirement already satisfied: seaborn in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (0.13.2) Requirement already satisfied: numpy!=1.24.0,>=1.20 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from seaborn) (2.0.2) Requirement already satisfied: pandas>=1.2 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from seaborn) (2.2.2) Requirement already satisfied: matplotlib!=3.6.1,>=3.4 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from seaborn) (3.9.2) Requirement already satisfied: contourpy>=1.0.1 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from matplotlib!=3.6.1,>=3.4->seaborn) (1.3.0) Requirement already satisfied: cycler>=0.10 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from matplotlib!=3.6.1,>=3.4->seaborn) (0.12.1) Requirement already satisfied: fonttools>=4.22.0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from matplotlib!=3.6.1,>=3.4->seaborn) (4.53.1) Requirement already satisfied: kiwisolver>=1.3.1 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from matplotlib!=3.6.1,>=3.4->seaborn) (1.4.7) Requirement already satisfied: packaging>=20.0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from matplotlib!=3.6.1,>=3.4->seaborn) (24.1) Requirement already satisfied: pillow>=8 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from matplotlib!=3.6.1,>=3.4->seaborn) (10.4.0) Requirement already satisfied: pyparsing>=2.3.1 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from matplotlib!=3.6.1,>=3.4->seaborn) (3.1.4) Requirement already satisfied: python-dateutil>=2.7 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from matplotlib!=3.6.1,>=3.4->seaborn) (2.9.0.post0) Requirement already satisfied: pytz>=2020.1 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pandas>=1.2->seaborn) (2024.1) Requirement already satisfied: tzdata>=2022.7 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pandas>=1.2->seaborn) (2024.1) Requirement already satisfied: six>=1.5 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from python-dateutil>=2.7->matplotlib!=3.6.1,>=3.4->seaborn) (1.16.0)
In [46]:
# grafico
import seaborn as sea
sea.boxplot(prov_Ag_map.year2022, color='yellow',orient='h')
Out[46]:
<Axes: xlabel='year2022'>
Mas del 70% de las provincias no llegan al 40% de realización de pruebas antigenas para detectar covid 19.
In [47]:
#grafica de trasnformacion cuantilica?
from sklearn.preprocessing import QuantileTransformer
qt = QuantileTransformer(n_quantiles=100, random_state=0,output_distribution='normal')
qt_result=qt.fit_transform(prov_Ag_map[['year2022']])
sea.boxplot(qt_result, color='yellow',orient='h')
Out[47]:
<Axes: >
In [48]:
prov_Ag_map['year_2022_qt']= qt_result
Correlación Espacial¶
Vecindario
In [49]:
!pip install pysal
Requirement already satisfied: pysal in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (24.7) Requirement already satisfied: beautifulsoup4>=4.10 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (4.12.3) Requirement already satisfied: geopandas>=0.10.0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (1.0.1) Requirement already satisfied: numpy>=1.22 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (2.0.2) Requirement already satisfied: packaging>=22 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (24.1) Requirement already satisfied: pandas>=1.4 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (2.2.2) Requirement already satisfied: platformdirs>=2.0.2 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (3.10.0) Requirement already satisfied: requests>=2.27 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (2.32.3) Requirement already satisfied: scipy>=1.8 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (1.14.1) Requirement already satisfied: shapely>=2.0.1 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (2.0.6) Requirement already satisfied: scikit-learn>=1.1 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (1.5.1) Requirement already satisfied: libpysal>=4.12.0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (4.12.1) Requirement already satisfied: access>=1.1.9 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (1.1.9) Requirement already satisfied: esda>=2.6.0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (2.6.0) Requirement already satisfied: giddy>=2.3.5 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (2.3.5) Requirement already satisfied: inequality>=1.0.1 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (1.0.1) Requirement already satisfied: pointpats>=2.5.0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (2.5.1) Requirement already satisfied: segregation>=2.5 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (2.5) Requirement already satisfied: spaghetti>=1.7.6 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (1.7.6) Requirement already satisfied: mgwr>=2.2.1 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (2.2.1) Requirement already satisfied: momepy>=0.7.2 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (0.8.1) Requirement already satisfied: spglm>=1.1.0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (1.1.0) Requirement already satisfied: spint>=1.0.7 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (1.0.7) Requirement already satisfied: spreg>=1.5.0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (1.7) Requirement already satisfied: spvcm>=0.3.0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (0.3.0) Requirement already satisfied: tobler>=0.11.2 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (0.11.3) Requirement already satisfied: mapclassify>=2.7.0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (2.8.0) Requirement already satisfied: splot>=1.1.5.post1 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (1.1.7) Requirement already satisfied: spopt>=0.6.1 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pysal) (0.6.1) Requirement already satisfied: soupsieve>1.2 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from beautifulsoup4>=4.10->pysal) (2.5) Requirement already satisfied: pyogrio>=0.7.2 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from geopandas>=0.10.0->pysal) (0.9.0) Requirement already satisfied: pyproj>=3.3.0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from geopandas>=0.10.0->pysal) (3.6.1) Requirement already satisfied: quantecon>=0.4.7 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from giddy>=2.3.5->pysal) (0.7.2) Requirement already satisfied: networkx>=2.7 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from mapclassify>=2.7.0->pysal) (3.3) Requirement already satisfied: tqdm>=4.63.0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from momepy>=0.7.2->pysal) (4.66.5) Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pandas>=1.4->pysal) (2.9.0.post0) Requirement already satisfied: pytz>=2020.1 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pandas>=1.4->pysal) (2024.1) Requirement already satisfied: tzdata>=2022.7 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pandas>=1.4->pysal) (2024.1) Requirement already satisfied: matplotlib>=3.6 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from pointpats>=2.5.0->pysal) (3.9.2) Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from requests>=2.27->pysal) (3.3.2) Requirement already satisfied: idna<4,>=2.5 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from requests>=2.27->pysal) (3.7) Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from requests>=2.27->pysal) (2.2.2) Requirement already satisfied: certifi>=2017.4.17 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from requests>=2.27->pysal) (2024.8.30) Requirement already satisfied: joblib>=1.2.0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from scikit-learn>=1.1->pysal) (1.4.2) Requirement already satisfied: threadpoolctl>=3.1.0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from scikit-learn>=1.1->pysal) (3.5.0) Requirement already satisfied: deprecation in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from segregation>=2.5->pysal) (2.1.0) Requirement already satisfied: seaborn in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from segregation>=2.5->pysal) (0.13.2) Requirement already satisfied: numba in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from segregation>=2.5->pysal) (0.60.0) Requirement already satisfied: rtree>=1.0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from spaghetti>=1.7.6->pysal) (1.3.0) Requirement already satisfied: pulp>=2.7 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from spopt>=0.6.1->pysal) (2.9.0) Requirement already satisfied: rasterio in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from tobler>=0.11.2->pysal) (1.4.1) Requirement already satisfied: statsmodels in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from tobler>=0.11.2->pysal) (0.14.4) Requirement already satisfied: rasterstats in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from tobler>=0.11.2->pysal) (0.20.0) Requirement already satisfied: contourpy>=1.0.1 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from matplotlib>=3.6->pointpats>=2.5.0->pysal) (1.3.0) Requirement already satisfied: cycler>=0.10 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from matplotlib>=3.6->pointpats>=2.5.0->pysal) (0.12.1) Requirement already satisfied: fonttools>=4.22.0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from matplotlib>=3.6->pointpats>=2.5.0->pysal) (4.53.1) Requirement already satisfied: kiwisolver>=1.3.1 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from matplotlib>=3.6->pointpats>=2.5.0->pysal) (1.4.7) Requirement already satisfied: pillow>=8 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from matplotlib>=3.6->pointpats>=2.5.0->pysal) (10.4.0) Requirement already satisfied: pyparsing>=2.3.1 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from matplotlib>=3.6->pointpats>=2.5.0->pysal) (3.1.4) Requirement already satisfied: six>=1.5 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from python-dateutil>=2.8.2->pandas>=1.4->pysal) (1.16.0) Requirement already satisfied: sympy in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from quantecon>=0.4.7->giddy>=2.3.5->pysal) (1.13.3) Requirement already satisfied: llvmlite<0.44,>=0.43.0dev0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from numba->segregation>=2.5->pysal) (0.43.0) Requirement already satisfied: colorama in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from tqdm>=4.63.0->momepy>=0.7.2->pysal) (0.4.6) Requirement already satisfied: affine in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from rasterio->tobler>=0.11.2->pysal) (2.4.0) Requirement already satisfied: attrs in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from rasterio->tobler>=0.11.2->pysal) (23.1.0) Requirement already satisfied: click>=4.0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from rasterio->tobler>=0.11.2->pysal) (8.1.7) Requirement already satisfied: cligj>=0.5 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from rasterio->tobler>=0.11.2->pysal) (0.7.2) Requirement already satisfied: click-plugins in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from rasterio->tobler>=0.11.2->pysal) (1.1.1) Requirement already satisfied: fiona in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from rasterstats->tobler>=0.11.2->pysal) (1.10.1) Requirement already satisfied: simplejson in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from rasterstats->tobler>=0.11.2->pysal) (3.19.3) Requirement already satisfied: patsy>=0.5.6 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from statsmodels->tobler>=0.11.2->pysal) (0.5.6) Requirement already satisfied: mpmath<1.4,>=1.1.0 in c:\users\camil\anaconda3\envs\herramientas\lib\site-packages (from sympy->quantecon>=0.4.7->giddy>=2.3.5->pysal) (1.3.0)
In [50]:
import numpy as np
print(np.__version__)
2.0.2
In [51]:
pip install numpy==1.23.4
Collecting numpy==1.23.4 Using cached numpy-1.23.4.tar.gz (10.7 MB) Installing build dependencies: started Installing build dependencies: finished with status 'done' Getting requirements to build wheel: started Getting requirements to build wheel: finished with status 'error' Note: you may need to restart the kernel to use updated packages.
error: subprocess-exited-with-error
Getting requirements to build wheel did not run successfully.
exit code: 1
[33 lines of output]
Traceback (most recent call last):
File "C:\Users\camil\anaconda3\envs\herramientas\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 353, in <module>
main()
File "C:\Users\camil\anaconda3\envs\herramientas\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 335, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\camil\anaconda3\envs\herramientas\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 112, in get_requires_for_build_wheel
backend = _build_backend()
^^^^^^^^^^^^^^^^
File "C:\Users\camil\anaconda3\envs\herramientas\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 77, in _build_backend
obj = import_module(mod_path)
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\camil\anaconda3\envs\herramientas\Lib\importlib\__init__.py", line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1310, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 995, in exec_module
File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
File "C:\Users\camil\AppData\Local\Temp\pip-build-env-umy30tp6\overlay\Lib\site-packages\setuptools\__init__.py", line 16, in <module>
import setuptools.version
File "C:\Users\camil\AppData\Local\Temp\pip-build-env-umy30tp6\overlay\Lib\site-packages\setuptools\version.py", line 1, in <module>
import pkg_resources
File "C:\Users\camil\AppData\Local\Temp\pip-build-env-umy30tp6\overlay\Lib\site-packages\pkg_resources\__init__.py", line 2172, in <module>
register_finder(pkgutil.ImpImporter, find_on_path)
^^^^^^^^^^^^^^^^^^^
AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error
Getting requirements to build wheel did not run successfully.
exit code: 1
See above for output.
note: This error originates from a subprocess, and is likely not a problem with pip.
In [52]:
from libpysal.weights import Queen, Rook, KNN
In [53]:
# rook
w_rook = Rook.from_dataframe(prov_Ag_map,use_index=False)
In [54]:
# queen
w_queen = Queen.from_dataframe(prov_Ag_map,use_index=False)
In [55]:
# k nearest neighbors
w_knn = KNN.from_dataframe(prov_Ag_map, k=8) #quiero 8 mas cercanos a mi.
Moran's correlation¶
In [56]:
# needed for spatial correlation
w_queen.transform = 'R'
In [57]:
pd.DataFrame(*w_queen.full()).sum(axis=1) # 1 means both are neighbors
Out[57]:
0 1.0
1 1.0
2 1.0
3 1.0
4 1.0
...
191 1.0
192 1.0
193 1.0
194 1.0
195 1.0
Length: 196, dtype: float64
INDICE DE MORAN
Quiero ver si hay correlacion entre mi vecindario cocovid.ias.
In [58]:
from esda.moran import Moran
moranCOVID = Moran(prov_Ag_map['year_2022_qt'], w_queen)
moranCOVID.I,moranCOVID.p_sim
#correlacion a 0. y es significativa por es 0.001 ***
Out[58]:
(np.float64(0.19136370498258676), np.float64(0.001))
interpretacion:Correlación de baja (0.19), pero sà sale significativo. Entonces, el virus sà tiene una variable espacial.
Grafico
In [59]:
from splot.esda import moran_scatterplot
import matplotlib.pyplot as plt
fig, ax = moran_scatterplot(moranCOVID)
ax.set_xlabel('Covid_prueba_share')
ax.set_ylabel('SpatialLag_Covid_prueba_share')
Out[59]:
Text(0, 0.5, 'SpatialLag_Covid_prueba_share')
Otro grafico
In [60]:
# The scatterplot with local info
from esda.moran import Moran_Local
# calculate Moran_Local and plot
lisaCOVID = Moran_Local(y=prov_Ag_map['year_2022_qt'], w=w_knn,seed=2022)
fig, ax = moran_scatterplot(lisaCOVID,p=0.05)
ax.set_xlabel('Covid_prueba_share')
ax.set_ylabel('SpatialLag_Covid_prueba_share');
La mayorÃa de casos se encuentran en el cuadrante a la derecha. .
Mapa de peru
In [61]:
# the map with the spots and outliers
from splot.esda import lisa_cluster
f, ax = plt.subplots(1, figsize=(12, 12))
plt.title('Spots and Outliers')
fig = lisa_cluster(lisaCOVID,
prov_Ag_map,ax=ax,
legend_kwds={'loc': 'center left',
'bbox_to_anchor': (0.7, 0.6)})
In [62]:
lisaCOVID.q
Out[62]:
array([1, 1, 1, 1, 1, 1, 1, 4, 3, 3, 3, 3, 3, 3, 4, 3, 3, 4, 4, 3, 3, 3,
3, 3, 4, 3, 3, 1, 4, 1, 4, 1, 4, 1, 4, 3, 3, 3, 4, 3, 4, 3, 1, 3,
3, 1, 1, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 3, 2, 2, 1, 2, 2, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 4, 3, 3, 4, 3, 1, 1, 4, 3, 1, 1, 4, 4, 1, 1, 1, 1, 1, 2, 1, 1,
2, 1, 1, 1, 1, 2, 4, 1, 1, 3, 4, 4, 1, 4, 1, 1, 1, 1, 4, 3, 1, 1,
1, 1, 4, 3, 1, 1, 1, 1, 4, 1, 2, 1, 2, 4, 2, 4, 4, 2, 4, 1, 4, 1,
4, 1, 2, 4, 1, 1, 1, 2, 4, 3, 2, 4, 4, 4, 4, 1, 2, 3, 4, 4, 4, 1,
4, 2, 1, 1, 1, 1, 1, 1, 4, 4, 3, 3, 3, 1, 1, 1, 4, 2, 1, 3])
In [63]:
# significance
lisaCOVID.p_sim
Out[63]:
array([0.165, 0.301, 0.099, 0.016, 0.051, 0.129, 0.029, 0.209, 0.087,
0.002, 0.012, 0.012, 0.013, 0.004, 0.335, 0.11 , 0.005, 0.09 ,
0.017, 0.013, 0.079, 0.13 , 0.002, 0.017, 0.132, 0.003, 0.02 ,
0.152, 0.181, 0.441, 0.058, 0.114, 0.239, 0.075, 0.404, 0.084,
0.019, 0.098, 0.181, 0.066, 0.157, 0.132, 0.461, 0.333, 0.179,
0.023, 0.332, 0.009, 0.02 , 0.008, 0.031, 0.052, 0.164, 0.135,
0.028, 0.052, 0.005, 0.121, 0.005, 0.101, 0.083, 0.142, 0.066,
0.049, 0.047, 0.025, 0.04 , 0.298, 0.22 , 0.225, 0.451, 0.232,
0.301, 0.269, 0.26 , 0.265, 0.243, 0.38 , 0.305, 0.195, 0.021,
0.039, 0.029, 0.095, 0.014, 0.469, 0.015, 0.406, 0.5 , 0.059,
0.001, 0.013, 0.077, 0.006, 0.498, 0.015, 0.073, 0.408, 0.499,
0.016, 0.37 , 0.331, 0.015, 0.036, 0.173, 0.214, 0.172, 0.217,
0.028, 0.271, 0.223, 0.095, 0.085, 0.184, 0.035, 0.014, 0.373,
0.036, 0.145, 0.028, 0.231, 0.2 , 0.066, 0.309, 0.03 , 0.067,
0.333, 0.036, 0.017, 0.091, 0.242, 0.103, 0.475, 0.016, 0.159,
0.034, 0.017, 0.307, 0.144, 0.056, 0.369, 0.264, 0.093, 0.046,
0.031, 0.358, 0.11 , 0.141, 0.039, 0.357, 0.058, 0.264, 0.056,
0.112, 0.31 , 0.15 , 0.001, 0.234, 0.159, 0.003, 0.006, 0.122,
0.03 , 0.332, 0.456, 0.012, 0.081, 0.41 , 0.369, 0.481, 0.275,
0.327, 0.152, 0.392, 0.044, 0.083, 0.034, 0.136, 0.137, 0.21 ,
0.465, 0.42 , 0.078, 0.438, 0.001, 0.036, 0.218, 0.069, 0.149,
0.009, 0.011, 0.011, 0.327, 0.488, 0.333, 0.207])
In [64]:
# quadrant: 1 HH, 2 LH, 3 LL, 4 HL
pd.Series(lisaCOVID.q).value_counts()
Out[64]:
1 94 4 43 3 42 2 17 Name: count, dtype: int64
In [65]:
prov_Ag_map['COVID_quadrant']=[l if p <0.05 else 0 for l,p in zip(lisaCOVID.q,lisaCOVID.p_sim) ]
prov_Ag_map['COVID_quadrant'].value_counts()
#Agregar a la correlación espacial
Out[65]:
COVID_quadrant 0 131 1 32 3 21 4 9 2 3 Name: count, dtype: int64
In [66]:
#recodificar
labels = [ '0 no_sig', '1 hotSpot', '2 coldOutlier', '3 coldSpot', '4 hotOutlier']
prov_Ag_map['COVID_quadrant_names']=[labels[i] for i in prov_Ag_map['COVID_quadrant']]
prov_Ag_map['COVID_quadrant_names'].value_counts()
Out[66]:
COVID_quadrant_names 0 no_sig 131 1 hotSpot 32 3 coldSpot 21 4 hotOutlier 9 2 coldOutlier 3 Name: count, dtype: int64
In [67]:
from matplotlib import colors
myColMap = colors.ListedColormap([ 'ghostwhite', 'red', 'green', 'black','orange'])
f, ax = plt.subplots(1, figsize=(12,12))
plt.title('Spots and Outliers')
prov_Ag_map.plot(column='COVID_quadrant_names',
categorical=True,
cmap=myColMap,
linewidth=0.1,
edgecolor='white',
legend=True,
legend_kwds={'loc': 'center left',
'bbox_to_anchor': (0.7, 0.6)},
ax=ax)
# Remove axis
ax.set_axis_off()
# Display the map
plt.show()
In [69]:
import folium
map1=prov_Ag_map[prov_Ag_map.COVID_quadrant_names=='1 hotSpot']
map2=prov_Ag_map[prov_Ag_map.COVID_quadrant_names=='2 coldOutlier']
map3=prov_Ag_map[prov_Ag_map.COVID_quadrant_names=='3 coldSpot']
map4=prov_Ag_map[prov_Ag_map.COVID_quadrant_names=='4 hotOutlier']
m = map1.explore(
color="red",
tooltip=False, # hide tooltip
popup=["location"], # (on-click)
name="hotSpot" # name of the layer in the map
)
map2.explore(
m=m, # notice
color="green",
tooltip=False,
popup=["location"],
name="coldOutlier"
)
map3.explore(
m=m,
color="black",
tooltip=False,
popup=["location"],
name="coldSpot",
)
map4.explore(
m=m,
color="orange",
tooltip=False,
popup=["location"],
name="hotOutlier",
)
folium.TileLayer("CartoDB positron", show=False).add_to(m) # use folium to add alternative tiles
folium.LayerControl(collapsed=True).add_to(m) # use folium to add layer control
m # show map
Out[69]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Bivariate Lisa
In [70]:
#from esda.moran import Moran_BV, Moran_Local_BV
from esda.moran import Moran_BV
mbi = Moran_BV(prov_Ag_map['year2021'], prov_Ag_map['year2022'], w_queen)
mbi.I,mbi.p_sim
Out[70]:
(np.float64(0.14224711545767468), np.float64(0.005))
In [71]:
# The scatterplot with local info
from esda.moran import Moran_Local_BV
# calculate Moran_Local and plot
lisaCOVID_bv = Moran_Local_BV(y=prov_Ag_map['year2021'],
x=prov_Ag_map['year2022'],
w=w_queen)
fig, ax = moran_scatterplot(lisaCOVID_bv, p=0.05,aspect_equal=True)
ax.set_xlabel('COVID_2022')
ax.set_ylabel('SpatialLag_COVID_2021')
plt.show()
In [72]:
prov_Ag_map['COVID_quadrant_21_22']=[l if p <0.05 else 0 for l,p in zip(lisaCOVID_bv.q,lisaCOVID_bv.p_sim) ]
labels = [ '0 no_sig', '1 hotSpot', '2 coldOutlier', '3 coldSpot', '4 hotOutlier']
prov_Ag_map['COVID_quadrant_21_22_names']=[labels[i] for i in prov_Ag_map['COVID_quadrant_21_22']]
In [73]:
# see new columns
prov_Ag_map
Out[73]:
| OBJECTID | geometry | location | DEPARTAMENTO | PROVINCIA_y | year2020 | year2021 | year2022 | year2023 | year2024 | flag | year_2022_qt | COVID_quadrant | COVID_quadrant_names | COVID_quadrant_21_22 | COVID_quadrant_21_22_names | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1.0 | POLYGON ((-77.72614 -5.94354, -77.72486 -5.943... | AMAZONAS+CHACHAPOYAS | AMAZONAS | CHACHAPOYAS | 1.000000 | 0.448919 | 0.414784 | 0.219178 | 0.322581 | both | 1.478159 | 0 | 0 no_sig | 0 | 0 no_sig |
| 1 | 2.0 | POLYGON ((-78.61909 -4.51001, -78.61802 -4.510... | AMAZONAS+BAGUA | AMAZONAS | BAGUA | 1.000000 | 0.144654 | 0.233813 | 0.240000 | 0.470588 | both | 1.027998 | 0 | 0 no_sig | 0 | 0 no_sig |
| 2 | 3.0 | POLYGON ((-77.72759 -5.1403, -77.72361 -5.1406... | AMAZONAS+BONGARA | AMAZONAS | BONGARA | 1.000000 | 0.291971 | 0.466019 | 0.454545 | 0.928571 | both | 1.720078 | 0 | 0 no_sig | 0 | 0 no_sig |
| 3 | 4.0 | POLYGON ((-77.81399 -2.99278, -77.81483 -2.995... | AMAZONAS+CONDORCANQUI | AMAZONAS | CONDORCANQUI | 1.000000 | 0.033333 | 0.025000 | 0.000000 | 0.000000 | both | -0.502255 | 1 | 1 hotSpot | 0 | 0 no_sig |
| 4 | 5.0 | POLYGON ((-78.13023 -5.9037, -78.13011 -5.9041... | AMAZONAS+LUYA | AMAZONAS | LUYA | 1.000000 | 0.036585 | 0.059361 | 0.111111 | 0.750000 | both | -0.140240 | 0 | 0 no_sig | 0 | 0 no_sig |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 191 | 192.0 | POLYGON ((-80.28521 -3.41276, -80.28406 -3.412... | TUMBES+ZARUMILLA | TUMBES | ZARUMILLA | 0.923077 | 0.315556 | 0.492958 | 0.588235 | 1.000000 | both | 1.827310 | 1 | 1 hotSpot | 0 | 0 no_sig |
| 192 | 193.0 | POLYGON ((-74.47145 -7.27617, -74.47052 -7.277... | UCAYALI+CORONELPORTILLO | UCAYALI | CORONEL PORTILLO | 0.996875 | 0.186831 | 0.503120 | 0.545455 | 0.515152 | both | 1.866251 | 0 | 0 no_sig | 4 | 4 hotOutlier |
| 193 | 194.0 | POLYGON ((-73.18146 -9.41174, -73.13475 -9.411... | UCAYALI+ATALAYA | UCAYALI | ATALAYA | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | both | -5.199338 | 0 | 0 no_sig | 0 | 0 no_sig |
| 194 | 195.0 | POLYGON ((-75.43663 -8.22999, -75.43651 -8.230... | UCAYALI+PADREABAD | UCAYALI | PADRE ABAD | 0.000000 | 0.078512 | 0.193694 | 0.000000 | 0.000000 | both | 0.778268 | 0 | 0 no_sig | 0 | 0 no_sig |
| 195 | 196.0 | POLYGON ((-70.6138 -9.87339, -70.6214 -9.87808... | UCAYALI+PURUS | UCAYALI | PURUS | 0.000000 | 0.000000 | 0.000000 | 1.000000 | 0.000000 | both | -5.199338 | 0 | 0 no_sig | 0 | 0 no_sig |
196 rows × 16 columns
In [74]:
from matplotlib import colors
myColMap = colors.ListedColormap([ 'ghostwhite', 'red', 'green', 'black','orange'])
f, ax = plt.subplots(1, figsize=(12,12))
plt.title('Spots and Outliers')
prov_Ag_map.plot(column='COVID_quadrant_21_22_names',
categorical=True,
cmap=myColMap,
linewidth=0.1,
edgecolor='white',
legend=True,
legend_kwds={'loc': 'center left',
'bbox_to_anchor': (0.7, 0.6)},
ax=ax)
# Remove axis
ax.set_axis_off()
# Display the map
plt.show()